<%-- 
    Document   : findResults
    Created on : Oct 26, 2011, 11:36:33 PM
    Author     : X@KER
--%>

<%@page import="javax.ejb.ObjectNotFoundException"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="manufacturerBean.ManufacturerHome"%>
<%@page import="manufacturerBean.Manufacturer"%>
<%@page import="categoryBean.Category"%>
<%@page import="categoryBean.CategoryHome"%>
<%@page import="goodsBean.Goods"%>
<%@page import="goodsBean.GoodsHome"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.HashMap"%>
<%@page import="DBConnection.ATeamObject"%>
<%@ page errorPage = "Error.jsp" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Результаты поиска</title>
        <style type="text/css">
        #head { /* Верхний блок */
        background: #333; /* Цвет фона */
        padding: 5px; /* Поля вокруг текста */
        color: #fff; /* Цвет текста */
        clear: both; /* Отменяем обтекание */
        }
        h1 {
        font-size: 36px; /* Размер шрифта */
        font-weight: bold; /* Жирное начертание */
        margin: 0; /* Убираем отступы */
        color: #fff; /* Цвет текста */
        }
        #foot { /* Нижний блок */
        background: #333; /* Цвет фона */
        padding: 5px; /* Поля вокруг текста */
        color: #fff; /* Цвет текста */
        clear: both; /* Отменяем обтекание */
        }
        </style>
        <%  InitialContext initialContext;
            GoodsHome goodsHome;
            Goods goods;
            Object homeObject;

            CategoryHome categoryHome;
            Category category;
            Object homeObject1;

            Manufacturer manufacturer;
            ManufacturerHome manufacturerHome;
            Object homeObject2;

            ATeamObject ato = new ATeamObject();

            initialContext = new InitialContext();
            homeObject = initialContext.lookup("GoodsBean");
            goodsHome = (GoodsHome) PortableRemoteObject.narrow(homeObject,
                    GoodsHome.class);

            homeObject1 = initialContext.lookup("CategoryBean");
            categoryHome = (CategoryHome) PortableRemoteObject.narrow(homeObject1,
                    CategoryHome.class);

            homeObject2 = initialContext.lookup("ManufacturerBean");
            manufacturerHome = (ManufacturerHome) PortableRemoteObject.narrow(homeObject2,
                    ManufacturerHome.class);
        %>
    </head>
    <body background="1.jpg" bgproperties="fixed">
        <div id="head">
        <center><h1>Результаты поиска</h1></center>
        </div>
        <h4><a href="index.jsp">Возврат на главную страницу</a></h4>
        <table>
            <tr>
                <td>
                    <form name="MainPageForm" method="post" action="findResults.jsp">
                        <center>
                            <h2>Поиск товаров</h2>
                            <table>
                                <tr>
                                    <td>
                                        <b>Наименование товара</b>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="text" name="GoodsName" size="50">
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="radio" name="SearchingCriteria" value="0" checked="true"> Совпадает
                                    </td>
                                </tr>
                                <tr>
                                    <td> 
                                        <input type="radio" name="SearchingCriteria" value="1"> Содержит
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Цена товара</b>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="text" name="GoodsPriceFrom" size="10">
                                        <input type="text" name="GoodsPriceTo" size="10" id="txt">
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="radio" name="GoodsPriceOptions" value=">" onclick="if(this.checked){confirmChoice(this.form,this.value);}"> Больше чем<br>
                                        <input type="radio" name="GoodsPriceOptions" value="<" onclick="if(this.checked){confirmChoice(this.form,this.value);}"> Меньше чем<br>
                                        <input type="radio" name="GoodsPriceOptions" value="=" checked="true" onclick="if(this.checked){confirmChoice(this.form,this.value);}"> Равно значению<br>
                                        <input type="radio" name="GoodsPriceOptions" value="BETWEEN" onclick="if(this.checked){confirmChoice(this.form,this.value);}"> Лежит в диапазоне<br>
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Производитель</b><td>
                                </tr>
                                <%  Collection allManufacturer = manufacturerHome.findAll();
                                    Iterator iter = allManufacturer.iterator();
                                %>
                                <tr>
                                    <td>
                                        <select name="ManufacturerSelect">
                                            <option></option>
                                            <% while (iter.hasNext()) {
                                                    manufacturer = (Manufacturer) iter.next();
                                            %>
                                            <option value="<%= manufacturer.getObjectId()%>"><%= manufacturer.getName()%></option>
                                            <% }%>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Категория товара</b></td>
                                </tr>
                                <% Collection allCategory = categoryHome.findAll();
                                    iter = allCategory.iterator();
                                %>
                                <tr>
                                    <td>
                                        <select name="GoodsCategorySelect">
                                            <option></option>
                                            <% while (iter.hasNext()) {
                                                    category = (Category) iter.next();

                                            %>
                                            <option value="<%= category.getObjectId()%>"><%= category.getName()%></option>
                                            <% }%>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="submit" value="Поиск">
                                        <input type="reset" value="Очистить">
                                    </td>
                                </tr> 
                            </table>
                        </center>
                    </form>
                </td>
                <td valign="top">
                    
                    <%  if (request.getParameter("GoodsName") != "") {
                            ato.setName(request.getParameter("GoodsName"));
                        }
                        if (request.getParameter("SearchingCriteria").equals("1") == true) {
                            ato.setContainName();
                        }
                        if (request.getParameter("GoodsPriceFrom") != "") {
                            ato.setPrice(new Integer(request.getParameter("GoodsPriceFrom")));
                        }
                        if (request.getParameter("GoodsPriceTo") != "") {
                            ato.setPriceMax(new Integer(request.getParameter("GoodsPriceTo")));
                        }
                        if (request.getParameter("GoodsPriceOptions").equals("=") == true) {
                            ato.setTypeOfPrice(1);
                        }
                        if (request.getParameter("GoodsPriceOptions").equals("<") == true) {
                            ato.setTypeOfPrice(2);
                        }
                        if (request.getParameter("GoodsPriceOptions").equals(">") == true) {
                            ato.setTypeOfPrice(3);
                        }
                        if (request.getParameter("GoodsPriceOptions").equals("BETWEEN") == true) {
                            ato.setTypeOfPrice(4);
                        }
                        if (request.getParameter("ManufacturerSelect") != "") {
                            ato.setManifacturerId(new Integer(request.getParameter("ManufacturerSelect")));
                        }
                        if (request.getParameter("GoodsCategorySelect") != "") {
                            ato.setParentId(new Integer(request.getParameter("GoodsCategorySelect")));
                        }
                        Collection allGoods = goodsHome.findByAllCriteria(ato);
                        iter = allGoods.iterator();
                        if (iter.hasNext()) {
                    %>
                    <center>
                        <table border = "1">
                            <TR>
                                
                                <TH>Товар</TH>
                                <TH>Категория</TH>
                                <TH>Производитель</TH>
                                <TH>Цена</TH>
                                <TH>Количество</TH>
                            </TR>
                            <% while(iter.hasNext()){
                                       goods = (Goods)iter.next(); %>
                                <TR>                                    
                                    <TD>
                                        <a href="goods.jsp?catID=<%= goods.getCategoryId() %>&catName=<%= goods.getNameOfCategory() %>&goodID=<%= goods.getGoodsId() %>&goodName=<%= goods.getNameOfGoods() %>">
                                            <%= goods.getNameOfGoods() %> 
                                        </a>
                                    </TD>
                                    <TD><%= goods.getNameOfCategory() %></TD>
                                    <TD><%= goods.getNameOfManufacture() %></TD>
                                    <TD><%= goods.getPrice() %></TD>
                                    <TD><%= goods.getNumber() %></TD>
                                </TR>
                            <%
                                }
                            %>
                        </table>
                    </center>
                    <%
                        } else {
                    %>
                    По вашему запросу ничего не найдено!
                    <%
                        }
                    %>
                </td>
            </tr>
        </table>
        
        <div id="foot">
        База данных магазина &copy; 2011-2012 
        </div>
    </body>
</html>
